package com.bungarus.test;

import com.bungarus.client.busi.EndUser;
import com.bungarus.codec.protobuf.MessageProtobuf;
import org.springframework.util.StringUtils;

/**
 * Test case for group message
 */
public class AccessTestGroupClient extends AbstractAccessTestClient{

    public static void main(String[] args) throws Exception {
        new AccessTestGroupClient().run(System.getProperty("access"));
    }

    protected void run(String accessPoint) {
        connectServer(accessPoint);
        while (true){
            String line = getInputString();
            MessageProtobuf.Message message;
            if(line.equalsIgnoreCase("login")) { //login
                message = TestClientMessageBuilder.buildLoginMessage(
                        "abc125",
                        "1003",
                        10000,
                        "Apo",
                        "Device",
                        "username3",
                        "password3"
                );
                // sending
                send(message);
            }
            else { // IM message
                String[] cmd = StringUtils.tokenizeToStringArray(line, " ");
                if(cmd.length > 1 && cmd[0].equals("start")) {
                    int totalCount = Integer.valueOf(cmd[2]);
                    int i = 1;
                    EndUser user = loggedInUser;
                    while (i <= totalCount) {
                        message = TestClientMessageBuilder.buildIMGroupMessage(
                                String.valueOf(System.currentTimeMillis()),
                                user,
                                "1000", // group id
                                "Apo",
                                "Device",
                                cmd[1] + " >>> " + i
                        );
                        i++;
                        send(message);
                    }
                }
                else {
                    message = TestClientMessageBuilder.buildIMGroupMessage(
                            String.valueOf(System.currentTimeMillis()),
                            loggedInUser,
                            "1000", // group id
                            "Apo",
                            "Device",
                            line
                    );
                    // sending
                    send(message);
                }
            }
        }
    }
}
